#!/bin/bash

# 定义变量
function variate() {
backup_sources_file_name="/etc/apt/sources.list.$(date +%Y%m%d%H%M%S).bak"
python_venv_dir="/path/to/venv"
root_multinode_file="/root/multinode"

globles_file="/etc/kolla/globles.yml"

# 获取脚本目录  当前脚本所在目录的绝对路径
mstack_script_dir=$(dirname "$(realpath "${BASH_SOURCE[0]}")")

# 获取 mstack 所在的父目录
mstack_parent_dir=$(dirname "$mstack_script_dir")


# 获取 mstack-packet 所在的目录
mstack_packet_dir=$mstack_parent_dir/mstack-packet
mstack_node_information_file="$mstack_parent_dir/node_information"
mstack_packet_archives_dir="$mstack_packet_dir/archives"
mstack_packet_docker_images="$mstack_packet_dir/docker_images"
mstack_packet_qcow2_images="$mstack_packet_dir/qcow2_image"
mstack_tools_dir="$mstack_script_dir/tools"
mstack_tools_mstack_dir="$mstack_tools_dir/mstack"
mstack_tools_nexus_dir="$mstack_tools_dir/nexus"
}

# 定义函数，获取 mstack 节点信息
function mstack_node_information() {
    mstack_all_nodes=$(sed -n '/\[mstack_all_node_file\]/,/\[mstack_all_node_file\]/p' $mstack_node_information_file |  \
    sed '/^\[mstack_all_node_file\]$/d;/^\s*#/d;/^\s*$/d')

    mstack_master_nodes_host_ip=$(sed -n '/\[mstack_all_node_file\]/,/\[mstack_all_node_file\]/p' $mstack_node_information_file | \
    sed '/^\[mstack_all_node_file\]$/d;/^\s*#/d;/^\s*$/d' | sed '1q' | awk '{print $1}')

    mstack_master_nodes_host_name=$(sed -n '/\[mstack_all_node_file\]/,/\[mstack_all_node_file\]/p' $mstack_node_information_file | \
    sed '/^\[mstack_all_node_file\]$/d;/^\s*#/d;/^\s*$/d' | sed '1q' | awk '{print $2}')

    mstack_slave_nodes=$(sed -n '/\[mstack_all_node_file\]/,/\[mstack_all_node_file\]/p' $mstack_node_information_file | \
    sed '/^\[mstack_all_node_file\]$/d;/^\s*#/d;/^\s*$/d' | sed '1d')

    mstack_all_nodes_information=$(echo "$mstack_all_nodes" > $mstack_tools_mstack_dir/mstack_all_nodes.txt && cat $mstack_tools_mstack_dir/mstack_all_nodes.txt)
    mstack_slave_nodes_information=$(echo "$mstack_slave_nodes" > $mstack_tools_mstack_dir/mstack_slave_nodes.txt && cat $mstack_tools_mstack_dir/mstack_slave_nodes.txt)
}

# 定义函数，获取 storage 节点信息
function storage_node_information() {
    storage_all_nodes=$(sed -n '/\[storage_all_node_file\]/,/\[storage_all_node_file\]/p' $mstack_node_information_file |  \
    sed '/^\[storage_all_node_file\]$/d;/^\s*#/d;/^\s*$/d')

    storage_master_nodes_host_ip=$(sed -n '/\[storage_all_node_file\]/,/\[storage_all_node_file\]/p' $mstack_node_information_file | \
    sed '/^\[storage_all_node_file\]$/d;/^\s*#/d;/^\s*$/d' | sed '1q' | awk '{print $1}')

    storage_master_nodes_host_name=$(sed -n '/\[storage_all_node_file\]/,/\[storage_all_node_file\]/p' $mstack_node_information_file | \
    sed '/^\[storage_all_node_file\]$/d;/^\s*#/d;/^\s*$/d' | sed '1q' | awk '{print $2}')

    storage_slave_nodes=$(sed -n '/\[storage_all_node_file\]/,/\[storage_all_node_file\]/p' $mstack_node_information_file | \
    sed '/^\[storage_all_node_file\]$/d;/^\s*#/d;/^\s*$/d' | sed '1d')

    storage_all_nodes_information=$(echo "$storage_all_nodes" > $mstack_tools_mstack_dir/storage_all_nodes.txt && cat $mstack_tools_mstack_dir/storage_all_nodes.txt)
    storage_slave_nodes_information=$(echo "$storage_slave_nodes" > $mstack_tools_mstack_dir/storage_slave_nodes.txt && cat $mstack_tools_mstack_dir/storage_slave_nodes.txt)
}

function storage_cluster_network_file() {
    storage_cluster_network=$(sed -n '/\[storage_cluster_network_file\]/,/\[storage_cluster_network_file\]/p' $mstack_node_information_file |  \
    sed '/^\[storage_cluster_network_file\]$/d;/^\s*#/d;/^\s*$/d'| \
    awk '{print $2}')
}

function mstack_multinode_config_file() {
    mstack_multinode_config=$(sed -n '/\[mstack_multinode_config_file\]/,/\[mstack_multinode_config_file\]/p' $mstack_node_information_file |  \
    sed '/^\[mstack_multinode_config_file\]$/d;/^\s*#/d;/^\s*$/d')

    mstack_multinode_config_information=$(echo "$mstack_multinode_config" > $mstack_tools_mstack_dir/mstack_multinode_config.txt && cat $mstack_tools_mstack_dir/mstack_multinode_config.txt)
}

function mstack_globals_config_file() {
    mstack_globals_config=$(sed -n '/\[mstack_globals_config_file\]/,/\[mstack_globals_config_file\]/p' $mstack_node_information_file |  \
    sed '/^\[mstack_globals_config_file\]$/d;/^\s*#/d;/^\s*$/d')
    mkdir -p /etc/kolla
    mstack_globals_config_information=$(echo "$mstack_globals_config" > $mstack_tools_mstack_dir/mstack_globals_config.txt && cat $mstack_tools_mstack_dir/mstack_globals_config.txt)
}

# 倒计时函数
countdown() {
    YELLOW='\033[0;33m'
    NC='\033[0m' # 无颜色

    # 接收第一个参数作为倒计时秒数，默认为 3
    local seconds=${1:-3}

    # 倒计时逻辑
    for ((i=seconds; i>0; i--)); do
        echo -ne "${YELLOW} ---倒计时剩余 ${i} 秒,请稍等---${NC}\r"
        sleep 1
    done
    echo -ne "\033[K"
## 使用方法 示例: 剩余5秒倒计时
#countdown 5
}

# 检查源文件 mstack_node_information_file 是否存在
check_source_file() {
    local source_file=$1 # 第一个参数作为源文件路径
    if [ ! -f "$mstack_node_information_file" ]; then
        echo -e "${RED}源文件 $mstack_node_information_file 不存在，请检查路径是否正确。${NC}"
        exit 1
    fi
    # 判断当前用户是否是 root
    if [ "$(whoami)" != "root" ]; then
        echo -e "${RED}当前用户不是 root，请切换到 root 用户后再执行脚本。${NC}"
        exit 1
    fi
}

# 检查虚拟环境目录是否存在
function check_venv_dir() {
# 设置虚拟环境的路径
python_venv_dir="/path/to/venv"
echo -e "${BLUE}检查虚拟环境路径是否存在...${NC}"
if [ ! -d "$python_venv_dir" ] || [ ! -f "$python_venv_dir/bin/activate" ]; then
    echo -e "${RED}虚拟环境不存在，请先创建！${NC}"
    exit 1
fi
}

# 定义颜色
function color() {
    RED='\033[0;31m'
    GREEN='\033[0;32m'
    YELLOW='\033[0;33m'
    BLUE='\033[0;34m'
    NC='\033[0m' # 无颜色
    CYAN='\e[1;36m'
}

# ------------------------
variate
color
mstack_node_information
storage_node_information
storage_cluster_network_file
check_source_file $mstack_node_information_file
mstack_multinode_config_file
mstack_globals_config_file


